package m.server.web.req;

import m.system.util.StringUtil;

import java.util.HashMap;
import java.util.Map;

public enum Header{
    Accept("Accept"),
    AcceptCharset("Accept-Charset"),
    AcceptEncoding("Accept-Encoding"),
    AcceptLanguage("Accept-Language"),
    AcceptRanges("Accept-Ranges"),
    AcceptPatch("Accept-Patch"),
    AccessControlAllowCredentials("Access-Control-Allow-Credentials"),
    AccessControlAllowHeaders("Access-Control-Allow-Headers"),
    AccessControlAllowMethods("Access-Control-Allow-Methods"),
    AccessControlAllowOrigin("Access-Control-Allow-Origin"),
    AccessControlAllowPrivateNetwork("Access-Control-Allow-Private-Network"),
    AccessControlExposeHeaders("Access-Control-Expose-Headers"),
    AccessControlMaxAge("Access-Control-Max-Age"),
    AccessControlRequestHeaders("Access-Control-Request-Headers"),
    AccessControlRequestMethod("Access-Control-Request-Method"),
    AccessControlRequestPrivateNetwork("Access-Control-Request-Private-Network"),
    Age("Age"),
    Allow("Allow"),
    Authorization("Authorization"),
    CacheControl("Cache-Control"),
    Connection("Connection"),
    ContentBase("Content-Base"),
    ContentEncoding("Content-Encoding"),
    ContentLanguage("Content-Language"),
    ContentLength("Content-Length"),
    ContentLocation("Content-Location"),
    ContentTransferEncoding("Content-Transfer-Encoding"),
    ContentDisposition("Content-Disposition"),
    ContentMd5("Content-Md5"),
    ContentRange("Content-Range"),
    ContentSecurityPolicy("Content-Security-Policy"),
    ContentType("Content-Type"),
    Cookie("Cookie"),
    Date("Date"),
    Dnt("Dnt"),
    Etag("Etag"),
    Expect("Expect"),
    Expires("Expires"),
    From("From"),
    Host("Host"),
    IfMatch("If-Match"),
    IfModifiedSince("If-Modified-Since"),
    IfNoneMatch("If-None-Match"),
    IfRange("If-Range"),
    IfUnmodifiedSince("If-Unmodified-Since"),
    LastModified("Last-Modified"),
    Location("Location"),
    MaxForwards("Max-Forwards"),
    Origin("Origin"),
    Pragma("Pragma"),
    ProxyAuthenticate("Proxy-Authenticate"),
    ProxyAuthorization("Proxy-Authorization"),
    Range("Range"),
    Referer("Referer"),
    RetryAfter("Retry-After"),
    SecWebsocketKey1("Sec-Websocket-Key1"),
    SecWebsocketKey2("Sec-Websocket-Key2"),
    SecWebsocketLocation("Sec-Websocket-Location"),
    SecWebsocketOrigin("Sec-Websocket-Origin"),
    SecWebsocketProtocol("Sec-Websocket-Protocol"),
    SecWebsocketVersion("Sec-Websocket-Version"),
    SecWebsocketKey("Sec-Websocket-Key"),
    SecWebsocketAccept("Sec-Websocket-Accept"),
    SecWebsocketExtensions("Sec-Websocket-Extensions"),
    Server("Server"),
    SetCookie("Set-Cookie"),
    SetCookie2("Set-Cookie2"),
    Te("Te"),
    Trailer("Trailer"),
    TransferEncoding("Transfer-Encoding"),
    Upgrade("Upgrade"),
    UpgradeInsecureRequests("Upgrade-Insecure-Requests"),
    UserAgent("User-Agent"),
    Vary("Vary"),
    Via("Via"),
    Warning("Warning"),
    WebsocketLocation("Websocket-Location"),
    WebsocketOrigin("Websocket-Origin"),
    WebsocketProtocol("Websocket-Protocol"),
    WwwAuthenticate("Www-Authenticate"),
    XFrameOptions("X-Frame-Options"),
    XRequestedWith("X-Requested-With"),
    XForwardedFor("X-Forwarded-For"),
    AltSvc("Alt-Svc"),
    ;
    private final String key;
    Header(String key){
        this.key=key;
    }
    public String getKey() {
        return key;
    }
    private static final Map<String,Header> keyMap=new HashMap<>();

    /**
     * 字符串转换枚举
     */
    public static Header to(String key){
        if(StringUtil.isSpace(key)) return null;
        Header h= keyMap.get(key);
        if(null==h){
            for(Header header : values()){
                if(key.equalsIgnoreCase(header.getKey())){
                    keyMap.put(key,header);
                    return header;
                }
            }
        }
        return h;
    }

    @Override
    public String toString() {
        return this.key;
    }
}
